package top.lyc829.leetcode.T21;


import java.util.List;

class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}


public class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {

        ListNode first = new ListNode(0, null);
        ListNode tmp = first;

        while (l1 != null && l2 != null) {
            if (l1.val < l2.val) {
                tmp.next = l1;
                l1 = l1.next;
            } else {
                tmp.next = l2;
                l2 = l2.next;
            }
            tmp = tmp.next;
        }

        tmp.next = l1 != null ? l1 : l2;


        return first.next;
    }
}